var events = require('../modules/const/EVENTS.js');
var redis = require('redis');
var config = require('../config');

module.exports = function(io){
	console.log("IO-EVENTS");
	var log = require("../lib/log").Create("WEBSOCKET_EVENT");
	log.debug(" ... GoGobeauty WebSocket Service ... ");
	var userId = null;

	var connectionHandle = function(socket){//具体逻辑处理
		if(config.redis.password != null){
			var sub = redis.createClient(config.redis.port, config.redis.ip, {password: config.redis.password});
		}else{
			var sub = redis.createClient(config.redis.port, config.redis.ip);
		}
		sub.on("message", function(channel, message){
			log.debug("Channel: "+channel+" Message: " + message);
			try{
				var msgObj = JSON.stringify(message);
			}catch(err){
				log.error(err);
				return;
			}
			//如果不是购买美丽金的支付则用 events.UPDATE
			var event = events.UPDATE;//默认情况下是订单通知
			if((/mlj_pay\d+/.test(channel))){
				event = channel;
			};
			if((/date\d+/.test(channel))){
				event = channel;
			};
			if((/order\d+/.test(channel))){
				event = channel;
			};
			if((/order_c\d+/.test(channel))){
				event = events.LOGIN_C_ORDER;
			};
			if(channel === events.WECHAT_CHECK_IN){
				event = channel;
			}
			if(channel === 'openapp'){
				event = channel;
			}
			if(channel === 'bigDataShare'){
				event = channel;
			}
			if(channel === 'todaydata'){
				event = channel;
			}
			log.debug("New message notify, select channel: %s, message: %s", event, message);
			socket.emit(event, message);
		});

		sub.on('error', function(error){
			log.error(error.message);
		});
		socket.on('todaydata',function(cb){
			log.debug(' todaydata connected todaydata' );
	
			cb({status:100, msg:'SUCCESS'});
			sub.subscribe('todaydata', function(err){
				if(err){
					log.error(err);
				}
			});
		});
		socket.on('openapp',function(cb){
			log.debug(' openapp connected openapp' );
	
			cb({status:100, msg:'SUCCESS'});
			sub.subscribe('openapp', function(err){
				if(err){
					log.error(err);
				}
			});
		});
		socket.on('bigDataShare',function(cb){
			log.debug(' bigDataShare connected bigDataShare' );
	
			cb({status:100, msg:'SUCCESS'});
			sub.subscribe('bigDataShare', function(err){
				if(err){
					log.error(err);
				}
			});
		});

		socket.on(events.DATE,function(userId,cb){
			log.debug('DATE：User %d connected', userId);
			userId = userId;
			cb({status:100, msg:'SUCCESS'});
			sub.subscribe(events.DATE+userId, function(err){
				log.debug("DATE_NOTIFY_LOGIN", userId);
				if(err){
					log.error(err);
				}
			});
		});

		socket.on(events.LOGIN_ORDER,function(userId,cb){
			log.debug('LOGIN_ORDER：User %d connected', userId);
			userId = userId;
			cb({status:100, msg:'SUCCESS'});
			sub.subscribe(events.LOGIN_ORDER+userId, function(err){
				log.debug("ORDER_NOTIFY_LOGIN: %d", userId);
				if(err){
					log.error(err);
				}
			});
		});

		socket.on(events.LOGIN_C_ORDER,function(userId,cb){
			log.debug('LOGIN_C_ORDER：User %d connected', userId);
			userId = userId;
			cb({status:100, msg:'SUCCESS'});
			sub.subscribe(events.LOGIN_C_ORDER+userId, function(err){
				log.debug("subscribe channel %s", events.LOGIN_C_ORDER+userId);
				if(err){
					log.error(err);
				}
			});
		});

		socket.on(events.LOGIN_ORDER,function(userId,cb){
			log.debug('LOGIN_ORDER：User %d connected', userId);
			userId = userId;
			cb({status:100, msg:'SUCCESS'});
			sub.subscribe(events.LOGIN_ORDER+userId, function(err){
				log.debug("ORDER_NOTIFY_LOGIN: %d", userId);
				if(err){
					log.error(err);
				}
			});
		});

		socket.on(events.MOBILE_LOGIN, function(uid, cb){
			userId = uid;
			log.debug('MOBILE_LOGIN：User %d connected', uid);
			cb({status:100, msg:'success'});
			sub.subscribe(events.NOTIFY+userId, function(err){
				if(err){
					log.error(err.message);
				}
			});
		});

		socket.on(events.MLJ_PAY, function(uid, cb){
			userId = uid;
			log.debug('MLJ_PAY：User %d connected', uid);
			cb({status:100, msg:'success'});
			sub.subscribe(events.MLJ_PAY+userId, function(err){
				log.debug('MLJ_PAY：User %d subscribe channel: '+events.MLJ_PAY+userId, userId);
				if(err){
					log.error(err);
				}
			});
		});

		socket.on(events.WEB_LOGIN, function(userId, cb){
			userId = userId;
			log.debug('WEB_LOGIN:User %d connected', userId);
			cb({status:100, msg:'success'});
			sub.subscribe(events.NOTIFY+userId, function(err){
				log.debug('WEB_LOGIN:User %d subscribe channel: '+events.NOTIFY+userId, userId);
				if(err){
					log.error(err);
				}
			});
		});

		socket.on(events.WECHAT_CHECK_IN, function(cb){
			log.debug('WECHAT_CHECK_IN connected');
			cb({status:100, msg:'success'});
			sub.subscribe(events.WECHAT_CHECK_IN, function(err){
				if(err){
					log.error(err);
					return;
				}
				log.debug('WECHAT_CHECK_IN subscribe channel %s', events.WECHAT_CHECK_IN);
			})
		});

		socket.on('disconnect', function(){
			if(sub){
				log.debug("disconnect:%d SUB QUIT ... ", userId);
				sub.unsubscribe();
				sub.quit();
			}
		});
	}

	io.on('connection', connectionHandle);
}